import { useIntersectionObserver, useIntervalFn } from '@vueuse/core'
import dayjs from 'dayjs'
import { ref } from 'vue'

export const userFn = (apiFn) => {
  const target = ref(null)
  const list = ref([])
  const { stop } = useIntersectionObserver(
    target,
    ([{ isIntersecting }]) => {
      if (isIntersecting) {
        // console.log(1111111111111111111)
        apiFn().then(({ result }) => {
          // console.log(result)
          list.value = result
        })
        stop()
      }
    }
  )

  return { list, target }
}
export const usePayTime = () => {
  const time = ref(0)
  const timeText = ref('')
  const { resume, pause } = useIntervalFn(() => {
    time.value--
    // 格式化后中文时间
    timeText.value = dayjs.unix(time.value).format('mm分ss秒')
    if (time.value <= 0) {
      pause()
    }
  }, 1000, { immediate: false })
  // number是接口那边返回来的数据
  const start = (number) => {
    // 返回来的数据给到time
    time.value = number
    // 格式化后中文时间
    // 然后这个是dayjs的
    timeText.value = dayjs.unix(time.value).format('mm分ss秒')
    // 重新开始
    resume()
  }
  return { time, timeText, start }
}
